/*
 * Copyright (C) 2003, Velocityme Partnership
 * UserAccessBean.java
 *
 * Created on 23 April 2003, 08:10
 */

package com.velocityme.entity;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import com.velocityme.interfaces.*;
import com.velocityme.interfaces.ServiceUnavailableException;

/**
 *
 * @author  Robert Crida Work
 * @ejb.bean
 *           type="CMP"
 *           cmp-version="2.x"
 *           name="UserAccess"
 *           jndi-name="ejb/UserAccess"
 *           view-type="local"
 * @ejb.transaction type="Required"
 *
 * @ejb.value-object match="*"
 *
 * @ejb.persistence table-name="useraccess"
 *
 * @ejb.util generate="physical"
 *
 * @ejb.finder signature="java.util.Collection findByNodeAndRoleAndUser(com.velocityme.interfaces.NodeLocal node, com.velocityme.interfaces.RoleLocal role, com.velocityme.interfaces.UserLocal user)"
 *             query="SELECT OBJECT(o) FROM UserAccess o WHERE o.nodeLocal = ?1 AND o.roleLocal = ?2 AND o.userLocal = ?3"
 *
 * @ejb.finder signature="java.util.Collection findByRoleAndUser(com.velocityme.interfaces.RoleLocal role, com.velocityme.interfaces.UserLocal user)"
 *             query="SELECT OBJECT(o) FROM UserAccess o WHERE o.roleLocal = ?1 AND o.userLocal = ?2"
 *
 * @ejb.finder signature="java.util.Collection findByUser(com.velocityme.interfaces.UserLocal user)"
 *             query="SELECT OBJECT(o) FROM UserAccess o WHERE o.userLocal = ?1"
 *
 * @ejb.finder signature="java.util.Collection findByNode(com.velocityme.interfaces.NodeLocal node)"
 *             query="SELECT OBJECT(o) FROM UserAccess o WHERE o.nodeLocal = ?1"
 *
 * @jboss.persistence create-table="true"
 *                    remove-table="false"
 **/
public abstract class UserAccessBean implements EntityBean {

    /**
    * Context set by container
    */
    private javax.ejb.EntityContext m_entityContext;

    public void setEntityContext(javax.ejb.EntityContext entityContext) {
        m_entityContext = entityContext;
    }

    public void unsetEntityContext() {
        m_entityContext = null;
    }
  
    /** @ejb.create-method 
     *  @ejb.interface-method view-type="local"
     */
    public com.velocityme.interfaces.UserAccessPK ejbCreate(com.velocityme.interfaces.NodeLocal p_nodeLocal,
                                                            com.velocityme.interfaces.UserLocal p_userLocal,
                                                            com.velocityme.interfaces.RoleLocal p_roleLocal) throws CreateException {
        try {
            setUserAccessId(generateNewID());
        }
        catch(ServiceUnavailableException e) {
            throw new EJBException(e.getMessage());
        }            
        // EJB 2.0 spec says return null for CMP ejbCreate methods.
        return null;
    }

    public void ejbPostCreate(com.velocityme.interfaces.NodeLocal p_nodeLocal,
                              com.velocityme.interfaces.UserLocal p_userLocal,
                              com.velocityme.interfaces.RoleLocal p_roleLocal) throws CreateException {
        // CMR relations must be set in ejbPostCreate
        setNodeLocal(p_nodeLocal);
        setUserLocal(p_userLocal);
        setRoleLocal(p_roleLocal);
    }

    /**
     * Retrive a unique creation id to use for this bean.  This will end up
     * demarcating this bean from others when it is stored as a record
     * in the database.
     *
     * @return Returns an integer that can be used as a unique creation id.
     *
     * @throws ServiceUnavailableException Indicating that it was not possible
     *                                     to retrieve a new unqiue ID because
     *                                     the service is not available
     **/
    private Integer generateNewID() throws ServiceUnavailableException {
        int lUniqueId = -1;
        try {            
            SequenceGenerator lBean = SequenceGeneratorUtil.getHome().create();
            lUniqueId = lBean.getNextNumber("useraccess","userAccessId");
            lBean.remove();
        }
        catch ( NamingException ne ) {
            throw new ServiceUnavailableException( "Naming lookup failure: " + ne.getMessage() );
        }
        catch ( CreateException ce ) {
            throw new ServiceUnavailableException( "Failure while creating a generator session bean: " + ce.getMessage() );
        }
        catch ( RemoveException re ) {
            // When the Bean cannot be removed after a while it will be taken back by the container
            // therefore ignore this exception
        }
        catch ( RemoteException rte ) {
            throw new ServiceUnavailableException( "Remote exception occured while accessing generator session bean: " +  rte.getMessage() );
        }
        
        return new Integer(lUniqueId);
    }
    
    /**
     *  This gets a local object from a remote object.
     *  @ejb.interface-method view-type="remote"
     */
    public com.velocityme.interfaces.UserAccessLocal getLocal() {
        return (UserAccessLocal)m_entityContext.getEJBLocalObject();
    }
    
    /** @ejb.interface-method view-type="local" */
    public abstract com.velocityme.valueobjects.UserAccessValue getUserAccessValue();
    /** @ejb.interface-method view-type="local" */
    public abstract void setUserAccessValue(com.velocityme.valueobjects.UserAccessValue p_value);
    
    //=======================================================================
    //
    //                   ATTRIBUTES
    //
    //=======================================================================
    
    /**
     * @ejb.persistence column-name="userAccessId"
     * @ejb.interface-method view-type="local"
     * @ejb.pk-field 
     */
    public abstract java.lang.Integer getUserAccessId();
    public abstract void setUserAccessId(java.lang.Integer userAccessId);
    
    //=======================================================================
    //
    //                   RELATIONSHIPS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="UserAccess-Node"
     *               role-name="UserAccess-has-a-Node"
     * @jboss.relation fk-column="nodeIdFk"
     *                 related-pk-field="nodeId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.NodeValue"
     *                   aggregate-name="NodeValue"
     */
    public abstract com.velocityme.interfaces.NodeLocal getNodeLocal(); 
    /** @ejb.interface-method view-type="local" */
    public abstract void setNodeLocal(com.velocityme.interfaces.NodeLocal nodeLocal);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="UserAccess-User"
     *               role-name="UserAccess-has-a-User"
     * @jboss.relation fk-column="userIdFk"
     *                 related-pk-field="userId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.UserValue"
     *                   aggregate-name="UserValue"
     */
    public abstract com.velocityme.interfaces.UserLocal getUserLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setUserLocal(com.velocityme.interfaces.UserLocal userLocal);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="UserAccess-Role"
     *               role-name="UserAccess-has-a-Role"
     * @jboss.relation fk-column="roleIdFk"
     *                 related-pk-field="roleId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.RoleValue"
     *                   aggregate-name="RoleValue"
     */
    public abstract com.velocityme.interfaces.RoleLocal getRoleLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setRoleLocal(com.velocityme.interfaces.RoleLocal roleLocal);
    
    //=======================================================================
    //
    //                   MANAGEMENT METHODS
    //
    //=======================================================================
    
}
